package can.bishe.modules.exam.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import can.bishe.common.utils.SubjectExeclReader;
import can.bishe.common.validator.ValidatorUtils;
import can.bishe.modules.exam.entity.*;
import can.bishe.modules.exam.service.*;
import can.bishe.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.R;



/**
 * 考试信息
 *
 * @author can
 * @email 822024417@qq.com
 * @date 2021-04-28 23:03:48
 */
@RestController
@RequestMapping("exam/examinfo")
public class ExamInfoController extends AbstractController {
    @Autowired
    private ExamInfoService examInfoService;
    @Autowired
    private ExamTestpaperService examTestpaperService;
    @Autowired
    private ExamSubjectService examSubjectService;
    @Autowired
    private ExamOptionService examOptionService;
    @Autowired
    private ExamSubjectOptionService examSubjectOptionService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = examInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 需要考试列表
     */
    @RequestMapping("/waitExamList")
    public R waitExamList(@RequestParam Map<String, Object> params){

        Long userId = getUserId();
        Long deptId = getDeptId();
        PageUtils page = examInfoService.queryWaitExamPage(params, userId, deptId);

        return R.ok().put("page", page);

    }
    /**
     * 抽取题库
     */
    @RequestMapping("/examBegin/{examId}")
    public R examBegin(@PathVariable Long examId){

        ExamInfoEntity examInfoEntity = examInfoService.getById(examId);
        Long tpId = examInfoEntity.getExamTpid();
        ExamTestpaperEntity examTestpaperEntity = examTestpaperService.getById(tpId);
        String tpName = examTestpaperEntity.getTpName();
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("tp_name",tpName);
        qw.select("tp_subjectid");
        List<ExamTestpaperEntity> examTestpaperEntities = examTestpaperService.list(qw);
        List<SubjectOptionDTO> subjectList = new ArrayList<>();
        for(ExamTestpaperEntity examTestpaperEntityOne:examTestpaperEntities) {
            SubjectOptionDTO subjectOptionDTO = new SubjectOptionDTO();
            List<ExamSubjectOptionEntity>  examSubjectOptionEntities = examSubjectOptionService.list(new QueryWrapper<ExamSubjectOptionEntity>().eq("subject_id", examTestpaperEntityOne.getTpSubjectid()));
            List<ExamOptionEntity> examOptionEntities = new ArrayList<>();
            for(ExamSubjectOptionEntity examSubjectOptionOne :examSubjectOptionEntities){
                examOptionEntities.add(examOptionService.getById(examSubjectOptionOne.getOptionId()));
            }
            subjectOptionDTO.setExamSubjectEntity(examSubjectService.getById(examTestpaperEntityOne.getTpSubjectid()));
            subjectOptionDTO.setExamOptionEntityList(examOptionEntities);
            subjectList.add(subjectOptionDTO);
        }

        return R.ok().put("subjectList",subjectList).put("examInfo",examInfoEntity);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{examId}")
    public R info(@PathVariable("examId") Long examId){
        ExamInfoEntity examInfo = examInfoService.getById(examId);

        return R.ok().put("examInfo", examInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExamInfoEntity examInfo){
        examInfoService.save(examInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ExamInfoEntity examInfo){
        ValidatorUtils.validateEntity(examInfo);
        examInfoService.updateById(examInfo);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] examIds){
        examInfoService.removeByIds(Arrays.asList(examIds));

        return R.ok();
    }

}
